package main

import (
	"context"
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"os"
	"tailf/proto"
)

var username, password, filename, addr string

func init() {
	flag.StringVar(&username, "username", "logtail", "username")
	flag.StringVar(&password, "password", "logtail", "password")
	flag.StringVar(&addr, "addr", "127.0.0.1:9090", "the server addr")
	flag.StringVar(&filename, "file", "", "the file name is tailed")
	flag.Parse()

	if filename == "" {
		fmt.Println("filename is required")
		flag.Usage()
		os.Exit(1)
	}
}

func main() {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.Close()

	client := proto.NewLogTailClient(conn)
	stream, err := client.Tail(context.Background(), &proto.LogRequest{
		Filename:             filename,
		Username:             username,
		Password:             password,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	for {
		resp, err := stream.Recv()
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Println(resp.GetLine())
	}
}
